package DingTalk

import (
	"github.com/kirinlabs/HttpRequest"
)

var httpClient *HttpRequest.Request

func init() {
	httpClient = HttpRequest.NewRequest()
	httpClient.SetTimeout(5)
}
func postJsonData(uri string, data map[string]interface{}) []byte {
	httpClient.SetHeaders(map[string]string{
		"Content-Type": "application/json",
	})
	params := map[string]interface{}{}
	for field, v := range data {
		params[field] = v
	}
	req, err := HttpRequest.JSON().Post(uri, params)
	if err != nil {
		return nil
	}
	if req.StatusCode() == 200 {
		body, err := req.Body()
		if err != nil {
			return nil
		}
		return body
	}
	return nil

}
func AlertToDingTalk(content string, uri string) {
	messageData := map[string]interface{}{
		"msgtype": "text",
		"text": map[string]interface{}{
			"content": content,
		},
	}
	postJsonData(uri, messageData)
}
